<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_user = false;

/*
用场景实现管理员和会员模式下不同的模型属性集
    public function scenarios()
    {
        return [
            'login' => ['username', 'password'],
            'register' => ['username', 'email', 'password'],
        ];
    }*/

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, '会员名或密码错误！');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return bool whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            $this->getUser();
            //未审核用户不许登录
            if($this->_user->status!=Member::STATUS_PROVED){
              $this->addError('username','您的注册申请已提交审核，请耐心等待...');
              return false;
            }
            if(Yii::$app->user->login($this->_user, $this->rememberMe ? 3600*24*30 : 0)){
              $user = Member::findOne($this->_user->getId());
              $user->generateAuthKey();
              if($user->save())
              {
                return true;
              }else{
                Yii::getLogger()->log(print_r($user->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
                return false;
              }
            }else{
              return false;
            }
        }
        return false;
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = Member::findByMemberName($this->username);
        }

        return $this->_user;
    }
    /**
     * @return array customized attribute labels
     */
     public function attributeLabels()
     {
         return [
           'username' => '用户名',
           'password' => '密码',
           'rememberMe' => '保持登录状态',

         ];
     }
}
